package parser;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import entities.*;


//<repAF> 
//<numRepAF>57</numRepAF> 
//<idFabrica>4</idFabrica> 
//<idCentro>18</idCentro> 
//<numSolFab>1054</numSolFab> 
//<articulos> 
//      <articuloFabrica> 
//              <ref>2</ref> 
//              <cant>1600</cant> 
//      </articuloFabrica> 
//      <articuloFabrica> 
//              <ref>6</ref> 
//              <cant>70</cant> 
//      </articuloFabrica> 
//</articulos> 
//</repAF>

public class WriterXML 
{
	public void generarReposiocnArt (List<CantidadArticulo> articulos, String idrepaf, String idfabrica, String idcentro, String idsolfab)
	{
		Element root = new Element("repAF");
	    
	    Element numrepaf = new Element("numRepAF");
		Element idfab = new Element ("idFabrica");
	    Element idcent = new Element ("idCentro");
	    Element numsolfab = new Element ("numSolFab");
		Element listarticulos = new Element ("articulos");
		
		numrepaf.addContent(idrepaf);
		idfab.addContent(idfabrica);
		idcent.addContent(idcentro);
		numsolfab.addContent(idsolfab);
		
		root.addContent(numrepaf);
	    root.addContent(idfab);
	    root.addContent(idcent);
	    root.addContent(numsolfab);
		
		for (CantidadArticulo aux : articulos) {
			Element ref = new Element ("ref");
			Element can = new Element ("can");
			ref.addContent(aux.getArticulo().getRef());
			can.addContent(String.valueOf(aux.getCantidadPedida()));
			Element articuloFab = new Element ("articuloFabrica");
			articuloFab.addContent(ref);
			articuloFab.addContent(can);
			listarticulos.addContent(articuloFab);
		}
	    
	    root.addContent(listarticulos);
	    
	    Document doc = new Document(root);

	    XMLOutputter out=new XMLOutputter();
	    
	    try {
			FileOutputStream file=new FileOutputStream("C:/PRUEBA-XML/repAF.xml");
			out.output(doc,file);
		    file.flush();
		    file.close();
		    out.output(doc,System.out);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	    
	}

}
